'use strict';

import Base from './base.js';
import qiniu from 'qiniu';
export default class extends Base {
    /**
     * index action
     * @return {Promise} []
     */
    init(http) {
        super.init(http);
        let uploadService = think.service('upload', 'upload');
        this.uploadService = new uploadService();
    }
    async indexAction() {
        let self = this;
        this.qconfig = this.config('qiniu');
        qiniu.conf.ACCESS_KEY = this.qconfig.access_key;
        qiniu.conf.SECRET_KEY = this.qconfig.secret_key;
        let bucket = this.qconfig.bucket;
        let key = this.uploadService.create() + '.png';
        let token = this.uploadService.uptoken(bucket, key);
        let filePath = this.file().file.path;
        let url = await this.uploadService.uploadFile(token, key, filePath, this.qconfig).then((data) => {
            return data;
        }).catch((err) => {
            throw ('图片上传失败')
        });

        if (url) {
            return self.success(url);
        } else {
            return self.fail(109);
        }
    }

}
